﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniRlv.COL;

namespace UniRlv.Utility.AppLib
{
    public class UniScanner
    {

        public List<string> m_saOpr = new List<string>();

        bool m_bZSM = true;//是否平时作为扫码入库使用，还是只是单纯的功能采集器
        LineDevice m_dev;
        en_uniscanner_steps m_oprType = en_uniscanner_steps.STEPS_SAVE;
        public en_uniscanner_steps m_enSteps;//当前操作码处理步骤：采集器扫描操作码后依次变化
        en_uniscanner_prc_state m_curPrcState = en_uniscanner_prc_state.CODE_OPR_OK;//当前码处理状态：保存状态还是操作码处理状态，还是待确认状态，等等

        string m_strErr;//操作结果字符串

        public string GetLastError()
        {
            if (m_curPrcState == en_uniscanner_prc_state.CODE_OPR_SCAN)
            {
                return GetOprTip(m_oprType);
            }
            else if (m_curPrcState == en_uniscanner_prc_state.CODE_OPR_NEED_OK)
            {
                return "请确认操作";
            }
            else
                return m_strErr;
        }

        public en_uniscanner_prc_state PutCode( string code )
        {
	        en_uniscanner_prc_state ps;

	        if(m_enSteps== en_uniscanner_steps.STEPS_SAVE)
	        {
		        if (code.Length==Const.FUNC_CODE_LENGTH)
		        {
			        ps = SetStepsCode(code);
			        return ps;
		        } 
		        else if(m_bZSM)
		        {
			        m_oprType = en_uniscanner_steps.STEPS_SAVE;
			        return en_uniscanner_prc_state.CODE_OK;
		        }
		        else
		        {
			        m_strErr = "请扫入功能码";
			        return en_uniscanner_prc_state.CODE_OPR_INVALID;
		        }
	        }
	        else
	        {
                if (code.Length == Const.FUNC_CODE_LENGTH)
		        {
			        return SetStepsCode(code);
		        } 
		        else
		        {
			        return StepsPrc(code);
		        }
	        }

	        return en_uniscanner_prc_state.CODE_OK;
        }

        public string GetOprTip(en_uniscanner_steps steps)
        {
            string strTip;

            switch (steps)
            {
                case en_uniscanner_steps.STEPS_DELETE:
                    strTip = "删除码操作。请扫描要删除的码"; break;
                case en_uniscanner_steps.STEPS_DELETE_BATCH:
                    strTip = string.Format("批量删除码操作。删除的码数量:{0}", m_saOpr.Count); break;
                case en_uniscanner_steps.STEPS_DELETE_PACKAGE:
                    strTip = "整包装删除操作。请扫描要删除的外包装码"; break;
                case en_uniscanner_steps.STEPS_SWITCH1:
                    strTip = "码位置互换操作。请扫描需要互换的两个码"; break;
                case en_uniscanner_steps.STEPS_SWITCH2:
                    strTip = "请继续扫描"; break;
                case en_uniscanner_steps.STEPS_REPLACE1:
                    strTip = "码替换操作。码替换操作：先扫旧码，再扫新码"; break;
                case en_uniscanner_steps.STEPS_INSERT1:
                    strTip = "码插入操作。码插入操作：先扫旧码，再扫新码"; break;
                case en_uniscanner_steps.STEPS_INSERT2:
                    strTip = "码插入操作。再扫新码"; break;
                case en_uniscanner_steps.STEPS_REPLACE2:
                    strTip = "再扫新码"; break;
                case en_uniscanner_steps.STEPS_CHECK1:
                    strTip = "查询码关联情况操作。查询码关联：先扫父码，再批量扫子码"; break;
                case en_uniscanner_steps.STEPS_CHECK2:
                    strTip = "再扫入子码"; break;
				case en_uniscanner_steps.STEPS_C_CHECK1:
					strTip = "连续查询父子码关联情况操作。连续查询父子码关联：先扫父码，再扫子码"; break;
				case en_uniscanner_steps.STEPS_C_CHECK2:
					strTip = "再扫入子码"; break;
				case en_uniscanner_steps.STEPS_QUERY_SINGLE:
                    strTip = "查询码状态操作。查询码状态：请扫描要查询的码"; break;
                case en_uniscanner_steps.STEPS_IMPLEMENT:
                    strTip = "补码操作。请扫描补码"; break;
                case en_uniscanner_steps.STEPS_DELETE_REMAIN:
                    strTip = "删除剩余未包装产品。确认删除？"; break;
                case en_uniscanner_steps.STEPS_UNBOX_REMAIN:
                    strTip = "设置剩余未包装产品散装。确认设置散装？"; break;
                case en_uniscanner_steps.STEPS_ZERO_REMAIN:
                    strTip = "设置剩余未包装产品零箱。清扫瞄零箱码"; break;
                case en_uniscanner_steps.STEPS_DELETE_EMPTY:
                    strTip = "删除空码操作。确认删除？"; break;
                case en_uniscanner_steps.STEPS_RESTART:
                    strTip = "重启任务操作操作。确认重启？"; break;
                case en_uniscanner_steps.STEPS_REPRINT://暂不支持打印
                    strTip = "扫描打印操作。扫描打印：请扫描要重打印的码"; break;
				case en_uniscanner_steps.STEPS_DELETE_SEC:
					strTip = "删除二次扫描码操作。请扫描需要删除的已二次扫描的码";break;
                default:
                    strTip = "暂不支持该操作码"; break;
            }

            return strTip;
        }

        public void SetDev( LineDevice dev, bool bZsm )
        {
	        m_dev = dev;
	        m_bZSM = bZsm;

            m_enSteps = en_uniscanner_steps.STEPS_SAVE;
        }

        public void Reset()
        {
            m_saOpr.Clear();
            m_oprType = en_uniscanner_steps.STEPS_SAVE;
            m_curPrcState = en_uniscanner_prc_state.CODE_OPR_OK;
        }

        public LineDevice GetDev()
        {
            return m_dev;
        }

        public en_uniscanner_steps GetOprType()
        {
	        return m_oprType;
        }

        /// <summary>
        /// [2015-12-21 ASUS]
        /// 方法描述：操作码步骤处理函数
        /// 参数描述：code：追溯码
        /// </summary>
        private en_uniscanner_prc_state StepsPrc(string code)
        {
	        en_uniscanner_prc_state ps = en_uniscanner_prc_state.CODE_OPR_SCAN;//默认继续扫描

	        switch(m_enSteps)
	        {
	        case en_uniscanner_steps.STEPS_SAVE://never happen
		        ps = en_uniscanner_prc_state.CODE_OK;
		        break;
	        case en_uniscanner_steps.STEPS_DELETE_BATCH:
			case en_uniscanner_steps.STEPS_CHECK2://批量查询关联
			case en_uniscanner_steps.STEPS_C_CHECK2://批量查询一二级关联
					m_saOpr.Add(code);
		        break;
	        case en_uniscanner_steps.STEPS_RESCAN_BOX:
	        case en_uniscanner_steps.STEPS_DELETE_REMAIN:
	        case en_uniscanner_steps.STEPS_DELETE_EMPTY:
	        case en_uniscanner_steps.STEPS_UNBOX_REMAIN:
		        ps = en_uniscanner_prc_state.CODE_OPR_INVALID;
		        m_strErr = "请直接扫描确认";
		        break;
	        case en_uniscanner_steps.STEPS_CHECK1:
		        m_saOpr.Add(code);
		        m_enSteps = en_uniscanner_steps.STEPS_CHECK2;
		        break;
			case en_uniscanner_steps.STEPS_C_CHECK1:
				m_saOpr.Add(code);
				m_enSteps = en_uniscanner_steps.STEPS_C_CHECK2;
				break;
			case en_uniscanner_steps.STEPS_INSERT1:
            m_saOpr.Add(code);
            m_enSteps = en_uniscanner_steps.STEPS_INSERT2;
            break;
	        case en_uniscanner_steps.STEPS_SWITCH1:
		        m_saOpr.Add(code);
		        m_enSteps = en_uniscanner_steps.STEPS_SWITCH2;
		        break;
	        case en_uniscanner_steps.STEPS_REPLACE1:
		        m_saOpr.Add(code);
		        m_enSteps = en_uniscanner_steps.STEPS_REPLACE2;
		        break;
	        //case en_uniscanner_steps.STEPS_CHECK2:
            case en_uniscanner_steps.STEPS_INSERT2:
	        case en_uniscanner_steps.STEPS_DELETE:
	        case en_uniscanner_steps.STEPS_DELETE_PACKAGE:
	        case en_uniscanner_steps.STEPS_SWITCH2:
	        case en_uniscanner_steps.STEPS_REPLACE2:
	        case en_uniscanner_steps.STEPS_QUERY_SINGLE:
	        case en_uniscanner_steps.STEPS_IMPLEMENT:
	        case en_uniscanner_steps.STEPS_REPRINT:
	        case en_uniscanner_steps.STEPS_ZERO_REMAIN:
			case en_uniscanner_steps.STEPS_DELETE_SEC:
				m_saOpr.Add(code);
		        ps = en_uniscanner_prc_state.CODE_OPR_NEED_OK;
		        m_curPrcState = ps;//变成待确认状态
		        break;
	        }

	        m_oprType = m_enSteps;

	        return ps;
        }


        private en_uniscanner_prc_state SetStepsCode(string code)
        {
	        en_uniscanner_steps us = GetStepsByCode(code);

            if (us == en_uniscanner_steps.STEPS_ERR)
	        {
		        Console.WriteLine("state switch to: error!\n");
		        return en_uniscanner_prc_state.CODE_OPR_INVALID;
	        }
            else if (us == en_uniscanner_steps.STEPS_OK)
	        {
                Console.WriteLine("state switch to: OK!\n");
                if (m_curPrcState == en_uniscanner_prc_state.CODE_OPR_NEED_OK ||
                    m_enSteps == en_uniscanner_steps.STEPS_DELETE_REMAIN ||
                    m_enSteps == en_uniscanner_steps.STEPS_DELETE_EMPTY ||
                    m_enSteps == en_uniscanner_steps.STEPS_UNBOX_REMAIN ||
                    m_enSteps == en_uniscanner_steps.STEPS_RESCAN_BOX ||
                    m_enSteps == en_uniscanner_steps.STEPS_DELETE_BATCH ||
					m_enSteps == en_uniscanner_steps.STEPS_CHECK2 ||
					m_enSteps == en_uniscanner_steps.STEPS_C_CHECK2 ||
					m_enSteps == en_uniscanner_steps.STEPS_RESTART ||
					m_enSteps == en_uniscanner_steps.STEPS_DELETE_SEC)
		        {
			        m_enSteps = en_uniscanner_steps.STEPS_SAVE;
                    m_curPrcState = en_uniscanner_prc_state.CODE_OPR_OK;
		        }
		        else
		        {
			        m_strErr = "操作错误。当前不允许扫描确认";
                    return en_uniscanner_prc_state.CODE_OPR_INVALID;
		        }

	        }
            else if (us == en_uniscanner_steps.STEPS_CANCEL)
	        {
                Console.WriteLine("state switch to: Cancel!\n");
                m_enSteps = en_uniscanner_steps.STEPS_SAVE;
		        m_saOpr.Clear();
                m_curPrcState = en_uniscanner_prc_state.CODE_OPR_CANCEL;
	        }
	        else
	        {
                Console.WriteLine("state switch to: {0}!\n", us);
		        m_enSteps = us;
		        m_oprType = us;
                m_curPrcState = en_uniscanner_prc_state.CODE_OPR_SCAN;
	        }

	        return m_curPrcState;
        }

        private en_uniscanner_steps GetStepsByCode( string code )
        {
	        if (code.Equals(Const.FUNC_REMOVE))
	        {
		        Console.WriteLine("state get: Delete!\n");
		        return en_uniscanner_steps.STEPS_DELETE;
	        } 
	        if (code.Equals(Const.FUNC_REMOVE_BATCH))
	        {
		        Console.WriteLine("state get: Delete batch!\n");
		        return en_uniscanner_steps.STEPS_DELETE_BATCH;
	        } 
	        if (code.Equals(Const.FUNC_REMOVE_EMPTY))
	        {
		        Console.WriteLine("state get: Delete empty!\n");
		        return en_uniscanner_steps.STEPS_DELETE_EMPTY;
	        } 
	        if (code.Equals(Const.FUNC_REMOVE_REMAIN))
	        {
		        Console.WriteLine("state get: Delete remain!\n");
		        return en_uniscanner_steps.STEPS_DELETE_REMAIN;
	        } 
	        if (code.Equals(Const.FUNC_REMOVE_PACKAGE))
	        {
		        Console.WriteLine("state get: Delete package!\n");
		        return en_uniscanner_steps.STEPS_DELETE_PACKAGE;
	        } 
	        else if(code.Equals(Const.FUNC_QUERY_CODE_STATE))
	        {
		        Console.WriteLine("state get: query single!\n");
		        return en_uniscanner_steps.STEPS_QUERY_SINGLE;
	        }
	        else if(code.Equals(Const.FUNC_CODE_SWITCH))
	        {
		        Console.WriteLine("state get: Swtich!\n");
		        return en_uniscanner_steps.STEPS_SWITCH1;
	        }
	        else if(code.Equals(Const.FUNC_CODE_REPLACE))
	        {
		        Console.WriteLine("state get: Replace!\n");
		        return en_uniscanner_steps.STEPS_REPLACE1;
	        }
	        else if(code.Equals(Const.FUNC_QUERY_RLV))
	        {
		        Console.WriteLine("state get: Query rlv!\n");
		        return en_uniscanner_steps.STEPS_CHECK1;
	        }
			else if (code.Equals(Const.FUNC_QUERY_RLV21))
			{//连续查询一二级码关联，2021-1-3
				Console.WriteLine("state get: Query rlv21!\n");
				return en_uniscanner_steps.STEPS_C_CHECK1;
			}
			else if(code.Equals(Const.FUNC_RESCAN_BOX))
	        {
		        Console.WriteLine("state get: rescan box!\n");
		        return en_uniscanner_steps.STEPS_RESCAN_BOX;
	        }
	        else if(code.Equals(Const.FUNC_RESTART))
	        {
		        Console.WriteLine("state get: restart!\n");
		        return en_uniscanner_steps.STEPS_RESTART;
	        }
	        else if(code.Equals(Const.FUNC_QUERY_CODE_STATE))
	        {
		        Console.WriteLine("state get: query single!\n");
		        return en_uniscanner_steps.STEPS_QUERY_SINGLE;
	        }
	        else if(code.Equals(Const.FUNC_UNBOX_REMAIN))
	        {
		        Console.WriteLine("state get: unbox remain!\n");
		        return en_uniscanner_steps.STEPS_UNBOX_REMAIN;
	        }
	        else if(code.Equals(Const.FUNC_ZERO_REMAIN))
	        {
		        Console.WriteLine("state get: zero remain!\n");
		        return en_uniscanner_steps.STEPS_ZERO_REMAIN;
	        }
	        else if(code.Equals(Const.FUNC_CODE_IMP))
	        {
		        Console.WriteLine("state get: implememt!\n");
		        return en_uniscanner_steps.STEPS_IMPLEMENT;
	        }
	        else if(code.Equals(Const.FUNC_OK))
	        {
		        Console.WriteLine("state get: ok!\n");
		        return en_uniscanner_steps.STEPS_OK;
	        }
	        else if(code.Equals(Const.FUNC_RE_PRINT))
	        {
		        Console.WriteLine("state get: reprint!\n");
		        return en_uniscanner_steps.STEPS_REPRINT;
	        }
	        else if(code.Equals(Const.FUNC_CANCEL))
	        {
		        Console.WriteLine("state get: cancel!\n");
		        m_strErr = "操作取消。取消操作，请继续扫描标签或者功能码";
		        return en_uniscanner_steps.STEPS_CANCEL;
	        }
            else if (code.Equals(Const.FUNC_INSERT))
	        {
		        Console.WriteLine("state get: insert!\n");
		        return en_uniscanner_steps.STEPS_INSERT1;
	        }
			else if (code.Equals(Const.FUNC_MANUAL_SCAN))
			{
				Console.WriteLine("state get: delete twice scan!\n");
				return en_uniscanner_steps.STEPS_DELETE_SEC;
			}
			else
	        {
		        return en_uniscanner_steps.STEPS_ERR;
	        }
        }
    }

}
